package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.ResourceMapper;
import com.board.dto.ResourceDto;
import com.board.entity.Resource;
import com.board.exception.ResourceException;
import com.board.param.ResourceAddParam;
import com.board.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourceDto> findAllResource() {
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq("level",1);
        List<Resource> resources = resourceMapper.selectList(wrapper);
        ArrayList<ResourceDto> list = new ArrayList<>();
        for (Resource resource : resources) {
            //递归查询下一级菜单
            ResourceDto dto = BeanUtil.toBean(resource, ResourceDto.class);
            findResourceNext(dto);
            list.add(dto);
        }


        return null;
    }

    @Override
    public Resource addResource(ResourceAddParam param) {
        //新增资源
        //名字不能重复，资源地址不能重复，parentId必须存在或为0，level必须为1,2,3之中一个
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",param.getName());
        if(param.getResourceUrl()!=""){//一级菜单传递的是“”，不用判断
            queryWrapper.or();
            queryWrapper.eq("resource_url",param.getResourceUrl());
        }

        List<Resource> resources = resourceMapper.selectList(queryWrapper);
        if(resources.size()>0) throw new ResourceException(500,"名字或资源地址已存在");
        if(param.getParentId()!=0){
            //不是一级菜单，要判断parentId是否存在
            Resource resource = resourceMapper.selectById(param.getParentId());
            if(resource==null) throw new ResourceException(500,"父资源不存在");
        }
        if(param.getLevel()!=1 && param.getLevel()!=2 && param.getLevel()!=3){
            throw new ResourceException(500,"资源级别不存在");
        }
        //对应的级别要和资源路径对应上
        if(param.getResourceUrl().equals("")){
            //没传资源路径：parentId必须为0，level必须为1，否则报错
            if(param.getParentId()!=0 || param.getLevel()!=1){
                throw new ResourceException(500,"资源地址和对应的级别不匹配");
            }
        }else{
            //传了资源路径：首先parentId不能为0， level不能为1
            if(param.getParentId()==0 || param.getLevel()==1){
                throw new ResourceException(500,"资源地址和对应的级别不匹配");
            }
            //parentId对象的parentId如果为0，level必须为2，否则必须为3
            Resource resource=resourceMapper.selectById(param.getParentId());
            if(resource.getParentId()==0){
                //表明新增的是二级菜单，level必须为2
                if(param.getLevel()!=2){
                    throw new ResourceException(500,"资源地址和对应的级别不匹配");
                }
            }else { //表明新增的是后端接口，level必须为3
                if(param.getLevel()!=3){
                    throw new ResourceException(500,"资源地址和对应的级别不匹配");
                }
            }
        }
        //传入的参数都正确，可以新增资源
        Resource resource = BeanUtil.toBean(param, Resource.class);
        resourceMapper.insert(resource);
        return resource;
    }

    private void findResourceNext(ResourceDto dto){
        List<Resource> childList = resourceMapper.selectList(new QueryWrapper<Resource>().eq("parent_id", dto.getId()));
        if (childList.size() > 0) {
            //还有子资源,可能有多个
            ArrayList<ResourceDto> list = new ArrayList<>();
            for (Resource resource : childList) {
                ResourceDto resourceDto = BeanUtil.toBean(resource, ResourceDto.class);
                //递归查询下一级菜单
                findResourceNext(resourceDto);
                list.add(resourceDto);
            }
            dto.setChildren(list);
        }
    }
}
